package com.zz.framework.core.ext

import java.util.ArrayList

/**
 * Copyright © 2022 StariverPool All rights reserved.
 * Created on 2022/4/1 17:10.
 * @version: V1.0
 */
//输入参数：source 原List ， splitItemNum 每个集合里放几个元素
fun  <T> List<T>.averageAssignFixLength( splitItemNum: Int): List<List<T>> {
    val source  = this
    val result = ArrayList<List<T>>()
    if (source != null && source.run { isNotEmpty() } && splitItemNum > 0) {
        if (source.size <= splitItemNum) {
            // 源List元素数量小于等于目标分组数量
            result.add(source)
        } else {
            // 计算拆分后list数量
            val splitNum = if (source.size % splitItemNum == 0) source.size / splitItemNum else source.size / splitItemNum + 1

            var value: List<T>? = null
            for (i in 0 until splitNum) {
                if (i < splitNum - 1) {
                    value = source.subList(i * splitItemNum, (i + 1) * splitItemNum)
                } else {
                    // 最后一组
                    value = source.subList(i * splitItemNum, source.size)
                }

                result.add(value)
            }
        }
    }
    return result
}

inline fun <T> ArrayList<T>.removeRange(index:Int, length:Int=-1):ArrayList<T> {
    if (length == -1)
        this.subList(index,this.size).clear()
    else
        this.subList(index,index+length).clear()
    return this
}

inline fun <T> MutableList<T>.removeRange(index:Int, length:Int=-1):MutableList<T> {
    if (length == -1)
        this.subList(index,this.size).clear()
    else
        this.subList(index,index+length).clear()
    return this
}